﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using System.Web;
namespace Config
{
    /// <summary>
    /// Lấy thông tin cấu hình từ File Web.Config
    /// </summary>
    public sealed class SettingSingleton
    {
        #region Singleton Pattern
        private static readonly SettingSingleton instance = new SettingSingleton();

        public static SettingSingleton Instance
        {
            get { return SettingSingleton.instance; }
        }
        #endregion

        #region Process Method
        /// <summary>
        /// Lấy giá trị của Key trong AppSetting
        /// </summary>
        /// <param name="_Key">Key</param>
        /// <returns>Return Null nếu không có key</returns>
        private string GetAppKeyValue(string _Key)
        {
            NameValueCollection appsettings = ConfigurationManager.AppSettings;
            if (appsettings == null) return null;
            if (appsettings.AllKeys.Contains(_Key))
            {
                return appsettings[_Key];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// Lấy xâu kết nối trong file Web.Config
        /// </summary>
        /// <param name="_Key">key</param>
        /// <returns>Return null nếu không có key</returns>
        private string GetConnectionStringKeyValue(string _Key)
        {
            try
            {
                return ConfigurationManager.ConnectionStrings[_Key].ConnectionString;
            }
            catch { }
            return null;
        }

        /// <summary>
        /// Xâu kết nối CSDL dùng cho WEBSERVICE kiểm duyệt BACKEND
        /// </summary>
        public string ConnectionStringWS
        {
            get
            {
                try
                {
                    return GetAppKeyValue("ConnectionString");
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
        }

        public string KeyPath
        {
            get
            {
                try
                {
                    return GetAppKeyValue("KEY_PATH");
                }
                catch (System.Exception)
                {
                    return string.Empty;
                }
            }
        }
        #endregion Process Method

        #region Attribute
        /// <summary>
        /// Xâu kết nối cơ sở dữ liệu
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return GetConnectionStringKeyValue(Constants.CONNECTION);
            }
        }

        /// <summary>
        /// Kiểm tra mã hóa MD5
        /// </summary>
        public bool IsMd5
        {
            get
            {
                return GetAppKeyValue(Constants.IS_MD5) == "true";
            }
        }

        /// <summary>
        /// Đường dẫn gốc của Web
        /// </summary>
        public string WebRoot
        {
            get
            {
                return System.Web.HttpContext.Current.Request.Url.Scheme + "://" +
                    System.Web.HttpContext.Current.Request.Url.Authority;
            }
        }

        /// <summary>
        /// Lấy đường dẫn hiện tại
        /// </summary>
        public string CurrentUrl
        {
            get
            {
                return WebRoot + System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            }
        }

        /// <summary>
        /// Get StoreID
        /// </summary>
        public int SiteID
        {
            get
            {
                try
                {
                    return Convert.ToInt32(GetAppKeyValue(Constants.SiteID));
                }
                catch (System.Exception)
                {
                    return 1;
                }
            }
        }

        public int EmailID
        {
            get
            {
                try
                {
                    return Convert.ToInt32(GetAppKeyValue(Constants.EmailID));
                }
                catch (System.Exception)
                {
                    return 0;
                }
            }
        }
        #endregion

        #region Resource Path
        public string ImagePath
        {
            get
            {
                try
                {
                    return GetAppKeyValue(Constants.ImagePath);
                }
                catch (System.Exception)
                {
                    return string.Empty;
                }
            }
        }

        public string GalleryPath
        {
            get
            {
                try
                {
                    return GetAppKeyValue(Constants.GalleryPath);
                }
                catch (Exception)
                {
                    return "";
                }
            }
        }
        #endregion

        #region Encryption
        public string SMTPPassKey
        {
            get
            {
                try
                {
                    return GetAppKeyValue(Constants.SMTPPassKey);
                }
                catch (System.Exception)
                {
                    return "";
                }
            }
        }

        public int SMTPPassIndex
        {
            get
            {
                try
                {
                    return Convert.ToInt32(GetAppKeyValue(Constants.SMTPPassIndex));
                }
                catch (System.Exception)
                {
                    return 0;
                }
            }
        }
        #endregion

        #region Mail
        /// <summary>
        /// Mail Server
        /// </summary>
        public string SMTPServer
        {
            get
            {
                return GetAppKeyValue(Constants.SMTP_SERVER);
            }
        }

        /// <summary>
        /// Mail Port
        /// </summary>
        public int SMTPPort
        {
            get
            {
                return Convert.ToInt32(GetAppKeyValue(Constants.SMTP_PORT));
            }
        }

        /// <summary>
        /// Mail Address
        /// </summary>
        public string EmailAddress
        {
            get
            {
                return GetAppKeyValue(Constants.EMAIL_ADDRESS);
            }
        }
        /// <summary>
        /// Mail UserName
        /// </summary>
        public string EmailUserName
        {
            get
            {
                return GetAppKeyValue(Constants.EMAIL_USERNAME);
            }
        }
        /// <summary>
        /// Mail Password
        /// </summary>
        public string EmailPassword
        {
            get
            {
                return GetAppKeyValue(Constants.EMAIL_PASSWORD);
            }
        }
        /// <summary>
        /// Mail Subject
        /// </summary>
        public string EmailSubject
        {
            get
            {
                return GetAppKeyValue(Constants.EMAIL_SUBJECT);
            }
        }
        #endregion
    }
}
